package com.efeichong.common.domain.entity.redis;

import com.efeichong.common.domain.RoleMenuVo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;
import org.springframework.data.redis.core.index.Indexed;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author lxk
 * @date 2020/9/2
 * @description
 */
@Data
@RedisHash
public class LoginUser implements UserDetails {
    @Id
    private Long id;
    private String username;
    @Indexed
    private String token;
    /**
     * 默认7天过期
     */
    @TimeToLive(unit = TimeUnit.DAYS)
    private Long expire = 7L;
    /**
     * 租户标识
     */
    private String tenantId;

    @Transient
    private Set<SimpleGrantedAuthority> authorities = new HashSet<>();

    private List<RoleMenuVo> permissions = new ArrayList<RoleMenuVo>();

    @JsonIgnore
    private String password;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
